package game;

import gameconsts.EditorConsts;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OptionsArea 
	extends JPanel
	implements ActionListener, Observer
{
	private static final long serialVersionUID = -2754617735003837635L;
	
	private JTextField fieldPosX = new JTextField(10);
	private JTextField fieldPosY = new JTextField(10);
	
	private JTextField fieldWidth = new JTextField(10);
	private JTextField fieldHeight = new JTextField(10);
	
	private JButton saveGameCompBtn = new JButton("Save component");
	
	private JButton saveGameLevelToDisk = new JButton("Save game to disk");
	
	private JButton loadGameLevelFromDisk = new JButton("Load game from disk");

	public OptionsArea()
	{
		setPreferredSize(new Dimension(EditorConsts.optionsAreaWidth, EditorConsts.defaultWindowHeight) );
		
		//why so many JPanels? *never* add components straight to another panel, if you want layouts to work as you intend
		//use 1 JPanel for every component
		Container mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS) );
		add(mainPanel);

		Container panelInputs = new JPanel();
		panelInputs.setLayout(new BoxLayout(panelInputs, BoxLayout.Y_AXIS) );

		panelInputs.add(createLabelForField("Position x:", fieldPosX) );
		panelInputs.add(createLabelForField("Position y:", fieldPosY) );
		panelInputs.add(createLabelForField("Width:", fieldWidth) );
		panelInputs.add(createLabelForField("Height:", fieldHeight) );

		mainPanel.add(panelInputs);

		Container panelButtons = new JPanel();
		panelButtons.setLayout(new BoxLayout(panelButtons, BoxLayout.Y_AXIS) );

		//"helper" panels p1, p2,... pn
		Container p1 = new JPanel();
		p1.setLayout(new FlowLayout(FlowLayout.LEADING) );
		saveGameCompBtn.addActionListener(this);
		saveGameCompBtn.setEnabled(false);
		p1.add(saveGameCompBtn);
		panelButtons.add(p1);
		
		Container p2 = new JPanel();
		p2.setLayout(new FlowLayout(FlowLayout.LEADING) );
		saveGameLevelToDisk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Game.saveGameLevelToDisk();
			}
		});
		p2.add(saveGameLevelToDisk);
		panelButtons.add(p2);

		Container p3 = new JPanel();
		p3.setLayout(new FlowLayout(FlowLayout.LEADING) );
		loadGameLevelFromDisk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Game.loadGameLevelFromDisk();
			}
		});
		p3.add(loadGameLevelFromDisk);
		panelButtons.add(p3);

		mainPanel.add(panelButtons);

		setVisible(true);
	}
	
//	protected void paintComponent(Graphics g)
//	{
//		g.setColor(Color.RED);
//		g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
//	}
	
	protected Container createLabelForField(String labelName, JTextField textField)
	{
		Container panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.TRAILING) );
		panel.add(new JLabel(labelName) );
		panel.add(textField);
		
		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
//		Game.selectedComp().x(Integer.parseInt(fieldPosX.getText() ) );
//		Game.selectedComp().y(Integer.parseInt(fieldPosY.getText() ) );
//		Game.selectedComp().width(Integer.parseInt(fieldWidth.getText() ) );
//		Game.selectedComp().height(Integer.parseInt(fieldHeight.getText() ) );
		
		Game.selectedComp().bounds(Integer.parseInt(fieldPosX.getText() ), Integer.parseInt(fieldPosY.getText() ),
								   Integer.parseInt(fieldWidth.getText() ), Integer.parseInt(fieldHeight.getText() ));
	}

	@Override
	public void update(Observable o, Object arg)
	{
		IDrawableRectangle gameComponent = (IDrawableRectangle) arg;

		fieldPosX.setText(String.valueOf(gameComponent.x() ) );
		fieldPosY.setText(String.valueOf(gameComponent.y() ) );

		fieldWidth.setText(String.valueOf(gameComponent.width() ) );
		fieldHeight.setText(String.valueOf(gameComponent.height() ) );

		saveGameCompBtn.setEnabled(true);
	}
}
